package com.yiyuzi.wiki.common;

import com.yiyuzi.wiki.common.constant.ResponseCodeEnum;

/**
 * 返回包装类
 *
 * @author yiyuzi
 * @date 2021/5/6 22:40
 */

public class ResponseDTO<T> {

    private Integer code;

    private String msg;

    private Boolean isSuccess;

    protected T data;

    public ResponseDTO() {
    }

    public ResponseDTO(ResponseCodeEnum responseCodeEnum, T data) {
        super();
        this.code = responseCodeEnum.getValue();
        this.msg = responseCodeEnum.getDesc();
        this.data = data;
        this.isSuccess =  responseCodeEnum == ResponseCodeEnum.SUCCESS;
    }

    private ResponseDTO(ResponseCodeEnum responseCodeEnum) {
        this.code = responseCodeEnum.getValue();
        this.msg = responseCodeEnum.getDesc();
        this.isSuccess =  responseCodeEnum == ResponseCodeEnum.SUCCESS;
    }

    public ResponseDTO(ResponseDTO responseDTO) {
        this.code = responseDTO.getCode();
        this.msg = responseDTO.getMsg();
        this.isSuccess = responseDTO.getSuccess();
        this.data = (T) responseDTO.getData();
    }

    public static <T> ResponseDTO<T> succ() {
        return new ResponseDTO<>(ResponseCodeEnum.SUCCESS);
    }

    public static <T> ResponseDTO<T> succData(T data) {
        return new ResponseDTO<>(ResponseCodeEnum.SUCCESS, data);
    }

    public static <T> ResponseDTO<T> err() {
        return new ResponseDTO<>(ResponseCodeEnum.FAIL);
    }

    public static <T> ResponseDTO<T> err(ResponseCodeEnum responseCodeEnum) {
        return new ResponseDTO<>(responseCodeEnum);
    }

    public static <T> ResponseDTO<T> res(boolean ans){
        if (ans){
            return succ();
        }else
            return err();
    }

    public static <T> ResponseDTO<T> res(boolean ans, ResponseCodeEnum responseCodeEnum){
        if (ans){
            return succ();
        }else
            return err(responseCodeEnum);
    }

    public static <T> ResponseDTO<T> res(boolean ans, T data){
        if (ans && data != null){
            return succData(data);
        }else
            return err();
    }

    public static <T> ResponseDTO<T> res(boolean ans, T data, ResponseCodeEnum responseCodeEnum){
        if (ans && data != null){
            return succData(data);
        }else
            return err(responseCodeEnum);
    }

    public static <T> ResponseDTO<T> res(T data){
        if (data != null){
            return succData(data);
        }else
            return err();
    }

    public static <T> ResponseDTO<T> res(T data, ResponseCodeEnum responseCodeEnum){
        if (data != null){
            return succData(data);
        }else
            return err(responseCodeEnum);
    }

    public String getMsg() {
        return msg;
    }

    public ResponseDTO setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public int getCode() {
        return code;
    }

    public ResponseDTO setCode(Integer code) {
        this.code = code;
        return this;
    }

    public T getData() {
        return data;
    }

    public ResponseDTO setData(T data) {
        this.data = data;
        return this;
    }

    public Boolean getSuccess() {
        return isSuccess;
    }

    public void setSuccess(Boolean success) {
        isSuccess = success;
    }

    @Override
    public String toString() {
        return "ResponseDTO{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data +
                '}';
    }
}
